package com.gxa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.entity.Order;
import com.gxa.feign.AccountFeignService;
import com.gxa.feign.StorageFeignService;
import com.gxa.mapper.OrderMapper;
import com.gxa.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private AccountFeignService accountFeignService;

    @Autowired
    private StorageFeignService storageFeignService;

    @GlobalTransactional //在谁的头上，谁就是TM
    @Override
    public void create(Order order) {
        //1、创建订单
        baseMapper.insert(order);

        //2、调用微服务扣减库存
        this.storageFeignService.decrease(order.getProductId(), order.getCount());
//在异常这里打个断点，可以看undoLog表，不要停太久，回滚久
//    int i = 5 / 0;

        //3、调用账户微服务扣减金额
        this.accountFeignService.decrease(order.getUserId(), order.getMoney());

        //4、修改订单状态
        baseMapper.update(order.getUserId(),0);
    }
}
